package cn.hxzy.controller;


import cn.hxzy.pojo.Dis;
import cn.hxzy.pojo.Reply;
import cn.hxzy.service.ReplyService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author songmengxin
 * @since 2021-08-03
 */
@RestController
@CrossOrigin
public class ReplyController {
    @Autowired
    private ReplyService replyService;

    @RequestMapping("/getReplyBydisId")
    public JSONObject getReplyBydisId(@RequestParam String disId) {
        List<Reply> list = replyService.getReplyBydisId(disId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "获取数据成功");
        jsonObject.put("count", list.size());
        jsonObject.put("data", list);
        return jsonObject;
    }

    @RequestMapping("/getReplyByreplyId")
    public JSONObject getReplyByreplyId(@RequestParam Integer replyId, @RequestParam String disId) {
        List<Reply> list = replyService.getReplyByreplyId(replyId, disId);
        String name = "";
        for (Reply reply : list) {
            name = reply.getUser().getName();
        }
        JSONObject jsonObject = new JSONObject();

        jsonObject.put("code", 0);
        jsonObject.put("msg", "获取数据成功");
        jsonObject.put("data", name);
        return jsonObject;
    }

    @RequestMapping("/insertReply")
    public JSONObject insertReply(@RequestBody Reply reply) {
        System.out.println(reply);
        boolean save = replyService.save(reply);
        JSONObject jsonObject = new JSONObject();
        if (save) {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "插入数据成功");
        } else {
            jsonObject.put("code", -1);
            jsonObject.put("msg", "获取数据失败");
        }
        return jsonObject;
    }
    @RequestMapping("/setLikeNumReplayByid")
    public JSONObject setLikeNumById(@RequestParam String replyId,@RequestParam Integer replyLikeNum){
        Reply reply=new Reply();
        reply.setReplyLikeNum(replyLikeNum);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("likeNum",replyLikeNum);
        queryWrapper.eq("proId",replyId);
        JSONObject jsonObject=new JSONObject();
        boolean update = replyService.update(reply, queryWrapper);
        jsonObject.put("code",0);
        jsonObject.put("msg","点赞成功");
        return  jsonObject;
    }
}

